package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Strategycatalog;
import cn.wolfcode.trip.base.mapper.StrategycatalogMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategycatalogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class StrategycatalogServiceImpl implements IStrategycatalogService {
    @Autowired
    private StrategycatalogMapper strategycatalogMapper ;

    public PageInfo<Strategycatalog> query(QueryObject qo) {
        //设置每页10条数据吧
        qo.setPageSize(10);
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Strategycatalog> list = strategycatalogMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void saveOrUpdate(Strategycatalog strategycatalog) {
        //这里有一个逻辑方法,前台是可以不用填写序号的，可以自动生成
        if(strategycatalog.getSequence()==null){
            //如果用户没有输入序号的话，我们这边就自动生成当前所属大攻略中的攻略分类的最大序号
            // ,赋值到这个攻略分类中去
            Integer sequence = strategycatalogMapper.
                    selectSequenceByStrategyId(strategycatalog.getStrategyId());
            //然后再判断当前这个大攻略中有没有存在分类，没有的话就将这个值设置为0
            if(sequence==null){
                sequence = 0 ;
            }
            strategycatalog.setSequence(sequence+1);
            //以上操作都是基于用户没有输入序号为前提，
        }
        //判断是新增还是修改,如果输入了，就直接调用新增和修改方法即可
        if(strategycatalog.getId()==null){
            strategycatalogMapper.insert(strategycatalog);
        }else{
            strategycatalogMapper.updateByPrimaryKey(strategycatalog);
        }
    }

    @Override
    public List<Strategycatalog> queryCatalogBystrategyId(Long strategyId) {
        return strategycatalogMapper.selectCatalogByStrategyId(strategyId);
    }


}
